﻿Imports System.Globalization

Partial Public Class frm_Tracking
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData()
    Dim clsFc As New ClassFunction
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "TRACKING"
            LoadDataTracking("", "", "", "")
        End If
    End Sub

    Private Sub LoadDataTracking(ByVal booking As String, ByVal Vessel As String, ByVal DateFrom As String, ByVal Dateto As String)
        Dim dt As New DataTable
        If (Session("Role") & "" = "00002") Or (Session("Role") & "" = "00007") Then
            dt = clsGet.Get_Tracking(CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Customer_no") & "", booking, Vessel, DateFrom, Dateto, CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("User_Name"))
        Else
            dt = clsGet.Get_Tracking("", booking, Vessel, DateFrom, Dateto, "")
        End If
        ViewState("Tracking") = dt
        gridTracking.DataSource = dt
        gridTracking.DataBind()
    End Sub

    Public Sub Paginate(ByVal sender As Object, ByVal e As CommandEventArgs)
        Dim CurrentIndex As Integer = gridTracking.PageIndex
        Select Case e.CommandArgument.ToString().ToLower()
            Case "first"
                gridTracking.PageIndex = 0
            Case "prev"
                If (gridTracking.PageIndex <> 0) Then
                    gridTracking.PageIndex = CurrentIndex - 1
                End If
            Case "next"
                If (gridTracking.PageIndex <> gridTracking.PageCount) Then
                    gridTracking.PageIndex = CurrentIndex + 1
                End If
            Case "last"
                gridTracking.PageIndex = gridTracking.PageCount
        End Select
        gridTracking.DataSource = ViewState("Tracking")
        gridTracking.DataBind()
    End Sub

    Protected Sub ddlPage_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim Page As GridViewRow = gridTracking.BottomPagerRow
        Dim ddlPage As DropDownList = CType(Page.Cells(0).FindControl("ddlPage"), DropDownList)
        gridTracking.PageIndex = ddlPage.SelectedIndex
        gridTracking.DataSource = ViewState("Tracking")
        gridTracking.DataBind()
    End Sub

    Public Function CreateDatatable() As DataTable
        Dim dt As New DataTable
        dt.Columns.Add("Booking_no", GetType(String))
        dt.Columns.Add("Vehicle_Text", GetType(String))
        dt.Columns.Add("Date_From", GetType(String))
        dt.Columns.Add("Date_To", GetType(String))
        dt.Columns.Add("Status", GetType(String))
        dt.Columns.Add("MAgent_no", GetType(String))
        dt.Columns.Add("PORT", GetType(String))
        dt.AcceptChanges()
        Return dt
    End Function

    Private Sub gridTracking_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridTracking.RowCommand
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        If (gridTracking.PageIndex <> 0) Then
            Index = Index Mod (gridTracking.PageIndex * 10)
        End If
        Dim dt As DataTable = CreateDatatable()
        Dim dr As DataRow = dt.NewRow
        ViewState("EditBooking") = gridTracking.DataKeys(Index).Values("Booking_no")
        dr.Item("Booking_no") = gridTracking.DataKeys(Index).Values("Booking_no")
        dr.Item("Vehicle_Text") = gridTracking.DataKeys(Index).Values("Vehicle_Text")
        dr.Item("Date_From") = gridTracking.DataKeys(Index).Values("Date_From")
        dr.Item("Date_To") = gridTracking.DataKeys(Index).Values("Date_To")
        dr.Item("Status") = gridTracking.DataKeys(Index).Values("Status")
        dr.Item("MAgent_no") = gridTracking.DataKeys(Index).Values("MAgent_no")
        dr.Item("PORT") = gridTracking.DataKeys(Index).Values("PORT")
        dt.Rows.Add(dr)
        dt.AcceptChanges()
        GridSub.DataSource = dt
        GridSub.DataBind()
        If e.CommandName = "Operation" Then
            panelMain.Visible = False
            PanelSub.Visible = True
            PanelOperation.Visible = True
            panelActual.Visible = False
            GetOperation(gridTracking.DataKeys(Index).Values("Booking_no") & "")
        ElseIf (e.CommandName = "Actual") Then
            panelMain.Visible = False
            PanelSub.Visible = True
            panelActual.Visible = True
            PanelOperation.Visible = False
            LiActual.Text = GetActual(gridTracking.DataKeys(Index).Values("Booking_no") & "", gridTracking.DataKeys(Index).Values("PORT") & "", gridTracking.DataKeys(Index).Values("MAgent_no") & "")
        ElseIf (e.CommandName = "Finish") Then

        End If
    End Sub

    Private Sub gridTracking_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridTracking.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            Dim imgOperation As ImageButton = CType(e.Row.FindControl("imgOperation"), ImageButton)
            Dim imgActual As ImageButton = CType(e.Row.FindControl("imgActual"), ImageButton)
            Dim imgFinish As ImageButton = CType(e.Row.FindControl("imgFinish"), ImageButton)

            'Operation
            Dim chkYellow As Boolean = False
            Dim chkCorrect As Boolean = True
            Dim dt_Log As DataTable = clsGet.Get_Booking_LogSheet_In(gridTracking.DataKeys(e.Row.RowIndex).Values("Booking_no") & "")
            If (dt_Log.Rows.Count > 0) Then
                If (dt_Log.Rows(0).Item("NOR_Date") & "" <> "") Then
                    chkYellow = True
                ElseIf (dt_Log.Rows(0).Item("ETA_Date") & "" <> "") Then
                    chkYellow = True
                Else
                    chkCorrect = False
                End If
                Dim dsBooking As DataTable = clsGet.Get_BookingOneRecord(gridTracking.DataKeys(e.Row.RowIndex).Values("Booking_no") & "").Tables(0)
                If (dsBooking.Rows.Count > 0) Then
                    If (dsBooking.Rows(0).Item("Lay_Time") & "" = "0") Then
                        rowPiliot.Visible = False
                    Else
                        If (dt_Log.Rows(0).Item("POB_Date") & "" <> "") Then
                            chkYellow = True
                        ElseIf (dt_Log.Rows(0).Item("Up_Date") & "" <> "") Then
                            chkYellow = True
                        Else
                            chkCorrect = False
                        End If
                    End If
                End If


                If (dt_Log.Rows(0).Item("Berthing_Date") & "" <> "") Then
                    chkYellow = True
                Else
                    chkCorrect = False
                End If
                dt_Log = clsGet.Get_Load_Unload_Product(gridTracking.DataKeys(e.Row.RowIndex).Values("Booking_no") & "", "")
                Dim p_start As Boolean = False
                Dim p_stop As Boolean = True
                If (dt_Log.Rows.Count > 0) Then
                    For Each dr As DataRow In dt_Log.Rows
                        If (dr.Item("LU_StartActual_Date") & "" <> "") Then
                            p_start = True
                        End If
                        If (dr.Item("LU_StopActual_Date") & "" = "") Then
                            p_stop = False
                        End If
                    Next
                    If (p_start) And (p_stop) Then

                        dt_Log = clsGet.Get_Booking_LogSheet_Out(gridTracking.DataKeys(e.Row.RowIndex).Values("Booking_no") & "")
                        If (dt_Log.Rows.Count > 0) Then
                            If (dt_Log.Rows(0).Item("Last_Date") & "" <> "") Then
                                chkYellow = True
                            Else
                                chkCorrect = False
                            End If
                        Else
                            chkCorrect = False
                        End If
                    ElseIf (p_start) Then
                        chkYellow = True
                        chkCorrect = False
                    Else
                        chkCorrect = False
                    End If
                Else
                    chkCorrect = False
                End If
            Else
                chkCorrect = False
            End If

            If (chkCorrect) Then
                imgOperation.ImageUrl = "../Image/Correct.png"
            ElseIf (chkYellow) Then
                imgOperation.ImageUrl = "../Image/Yellow.png"
            Else
                imgOperation.ImageUrl = "../Image/NotCorrect.png"
            End If

            If (gridTracking.DataKeys(e.Row.RowIndex).Values("Status") & "" = "FN") Then
                imgActual.ImageUrl = "../Image/Correct.png"
                imgFinish.ImageUrl = "../Image/Correct.png"
            Else
                imgActual.ImageUrl = "../Image/NotCorrect.png"
                imgFinish.ImageUrl = "../Image/NotCorrect.png"
            End If

            If (gridTracking.DataKeys(e.Row.RowIndex).Values("Date_From") & "" <> "") Then
                If (gridTracking.DataKeys(e.Row.RowIndex).Values("Date_From") & "" = gridTracking.DataKeys(e.Row.RowIndex).Values("Date_To") & "") Then
                    e.Row.Cells(2).Text = gridTracking.DataKeys(e.Row.RowIndex).Values("Date_To")
                Else
                    e.Row.Cells(2).Text = gridTracking.DataKeys(e.Row.RowIndex).Values("Date_From").ToString.Substring(0, 2) & "-" & gridTracking.DataKeys(e.Row.RowIndex).Values("Date_To")
                End If
            End If

        ElseIf (e.Row.RowType = DataControlRowType.Pager) Then
            Dim Page As GridViewRow = gridTracking.BottomPagerRow
            Dim ddlPage As DropDownList = CType(e.Row.FindControl("ddlPage"), DropDownList)
            Dim lblPageCount As Label = CType(e.Row.FindControl("lbPageCount"), Label)
            If Not (ddlPage Is Nothing) Then
                For i As Integer = 0 To gridTracking.PageCount - 1
                    Dim intPageNumber As Integer = i + 1
                    Dim IsItem As New ListItem(intPageNumber.ToString())
                    If (i = gridTracking.PageIndex) Then
                        IsItem.Selected = True
                    End If
                    ddlPage.Items.Add(IsItem)
                    ddlPage.Items(i).Attributes.Add("Style", "height:60px")
                Next

            End If
            If Not (lblPageCount Is Nothing) Then
                lblPageCount.Text = gridTracking.PageCount.ToString()
            End If
        End If
    End Sub

    Private Sub FormatN(ByRef data As String)
        Try
            data = FormatNumber(data, 2)
        Catch ex As Exception

        End Try
    End Sub

    Protected Function GetActual(ByVal Booking_no As String, ByVal PORT As String, ByVal MAgent_no As String) As String
        HeadReport(Booking_no)

        Dim HTML As String = ""
        HTML = "<table width='95%'  style='border-width: 0px;' border='1' cellpadding='0' cellspacing='0'>"
        HTML &= "<tr style='background-color:#32cd32;'>"
        HTML &= "<td style='width:140px;' valign='top'  align='center'><font class='White-Style-Head' style='color:#000000'>Code</font>&nbsp;</td>"
        HTML &= "<td style='width:280px;' align='center'><font class='White-Style-Head' style='color:#000000'>DESCRIPTION</font></td>"
        HTML &= "<td style='width:80px;' valign='bottom' align='center'><font class='White-Style-Head' style='color:#000000'>QTY</font>&nbsp;</td>"
        HTML &= "<td style='width:80px;' valign='bottom' align='center'><font class='White-Style-Head' style='color:#000000'>RATE/UNIT</font></td>"
        HTML &= "<td style='width:70px;' valign='bottom' align='center'><font class='White-Style-Head' style='color:#000000'>QTY</font>&nbsp;</td>"
        HTML &= "<td align='right'  valign='center'><font class='White-Style-Head' style='color:#000000'>AMOUNT</font></td>"
        HTML &= "</tr>"

        Dim Vat As String = ""
        Dim dsConfig As DataSet = Session("Config")
        If (dsConfig.Tables(0).Rows(0).Item("Vat").ToString().Split(".")(1) = "00") Then
            Vat = CInt(dsConfig.Tables(0).Rows(0).Item("Vat").ToString().Split(".")(0)).ToString()
        Else
            Vat = dsConfig.Tables(0).Rows(0).Item("Vat").ToString()
        End If

        Dim Value As Double = 0
        Dim ValueVat As Double = 0
        Dim WHT As Double = 0
        Dim row As Integer = 2
        Dim DSItem As New DataTable
        If (PORT = "L") Then
            DSItem = clsGet.GetActual_Data_Backup_For_Billing(Booking_no, "", False)
        Else
            DSItem = clsGet.GetActual_Data_Backup_For_Billing(Booking_no, MAgent_no, False)
        End If

        For Each dr As DataRow In DSItem.Rows
            If (CDbl(dr.Item("data4")) > 0.0) Then
                Dim MatName As String = dr.Item("Text_line")
                If (row Mod 2) Then
                    HTML &= "<tr>"
                Else
                    HTML &= "<tr style='background-color:#F0F9FB;'>"
                End If
                row += 1
                Dim data1 As String = dr.Item("data1")
                Dim data2 As String = dr.Item("data2")
                Dim data3 As String = dr.Item("data3")
                Dim data4 As String = dr.Item("data4")
                FormatN(data1)
                FormatN(data2)
                ' FormatN(data3)
                FormatN(data4)
                Dim Mat_Code As String = ""
                Try
                    Mat_Code = CInt(dr.Item("Material"))
                Catch ex As Exception
                    Mat_Code = dr.Item("Material")
                End Try
                HTML &= "<td style='width:140px;' valign='top'  align='left'><font class='White-Style' style='color:#000000'>" & Mat_Code & "</font>&nbsp;</td>"
                HTML &= "<td style='width:280px;' align='left'><font class='White-Style' style='color:#000000'>" & MatName & "</font></td>"
                HTML &= "<td style='width:80px;' valign='bottom' align='right'><font class='White-Style' style='color:#000000'>" & data1 & "</font>&nbsp;</td>"
                HTML &= "<td style='width:80px;' valign='bottom' align='right'><font class='White-Style' style='color:#000000'>" & data2 & "</font></td>"
                HTML &= "<td style='width:70px;' valign='bottom' align='center'><font class='White-Style' style='color:#000000'>" & data3 & "</font>&nbsp;</td>"
                HTML &= "<td align='right'  valign='bottom'><font class='White-Style' style='color:#000000'>" & data4 & "</font></td>"
                HTML &= "</tr>"
                Value = Value + CDbl(dr.Item("data4"))
                Dim dt As DataTable = clsGet.Get_MeterailWhithodingTax(dr.Item("Material"))
                If (dt.Rows(0).Item("MAT_WHT") & "" = "01") Or (dt.Rows(0).Item("MAT_WHT") & "" = "02") Or (dt.Rows(0).Item("MAT_WHT") & "" = "03") Then
                    '1%
                    WHT = (WHT + CDbl(((CDbl(dr.Item("data4")) * 1) / 100).ToString("#########0.00"))).ToString("###############0.00")
                ElseIf (dt.Rows(0).Item("MAT_WHT") & "" = "04") Or (dt.Rows(0).Item("MAT_WHT") & "" = "05") Then
                    '3%
                    WHT = (WHT + CDbl(((CDbl(dr.Item("data4")) * 3) / 100).ToString("#########0.00"))).ToString("###############0.00")
                ElseIf (dt.Rows(0).Item("MAT_WHT") & "" = "06") Or (dt.Rows(0).Item("MAT_WHT") & "" = "22") Then
                    '5%
                    WHT = (WHT + CDbl(((CDbl(dr.Item("data4")) * 5) / 100).ToString("#########0.00"))).ToString("###############0.00")
                End If
            End If

        Next


        HTML &= "<tr>"
        'HTML &= "<td style='border-width: 1px 0px 0px 0px;' colspan='5' align='left'>&nbsp;</td>"
        'HTML &= "<td align='right'><font class='White-Style' style='color:#000000'>" & FormatNumber(Value.ToString("#############0.00")) & "</font></td>"
        'HTML &= "</tr>"


        HTML &= "<td style='width:140px;' valign='top'  align='left'>&nbsp;</td>"
        HTML &= "<td style='width:280px;' align='left'><font class='White-Style' style='color:#000000'>TOTAL SERVICE CHARGE       (EXCLUSIVE OF VAT)</font></td>"
        HTML &= "<td style='width:80px;' valign='bottom' align='right'>&nbsp;</td>"
        HTML &= "<td style='width:80px;' valign='bottom' align='right'>&nbsp;</td>"
        HTML &= "<td style='width:70px;' valign='bottom' align='center'>&nbsp;</td>"
        HTML &= "<td align='right'  valign='bottom'><font class='White-Style' style='color:#000000'>" & FormatNumber(Value.ToString("#############0.00")) & "</font></td>"
        HTML &= "</tr>"

        ValueVat = ((Value * CDbl(Vat)) / 100).ToString("############.00")
        HTML &= "<tr>"
        'HTML &= "<td style='border-width: 0px;' colspan='5' align='right'><font class='White-Style' style='color:#000000'>Vat " & Vat & "%</font>&nbsp;</td>"
        'HTML &= "<td align='right'><font class='White-Style' style='color:#000000'>" & FormatNumber(ValueVat) & "</font></td>"
        'HTML &= "</tr>"
        HTML &= "<td style='width:140px;' valign='top'  align='left'>&nbsp;</td>"
        HTML &= "<td style='width:280px;' align='right'><font class='White-Style' style='color:#000000'>ADD VAT " & Vat & "%   </font></td>"
        HTML &= "<td style='width:80px;' valign='bottom' align='right'>&nbsp;</td>"
        HTML &= "<td style='width:80px;' valign='bottom' align='right'>&nbsp;</td>"
        HTML &= "<td style='width:70px;' valign='bottom' align='center'>&nbsp;</td>"
        HTML &= "<td align='right'  valign='bottom'><font class='White-Style' style='color:#000000'>" & FormatNumber(ValueVat, 2) & "</font></td>"
        HTML &= "</tr>"


        HTML &= "<tr>"
        'HTML &= "<td style='border-width: 0px;' colspan='5' align='right'><font class='White-Style' style='color:#000000'>รวมประเมินค่าใช้จ่าย</font>&nbsp;</td>"
        'HTML &= "<td align='right'><font class='White-Style' style='color:#000000'>" & FormatNumber((Value + ValueVat).ToString("###############.00")) & "</font></td>"
        'HTML &= "</tr>"
        HTML &= "<td style='width:140px;' valign='top'  align='left'>&nbsp;</td>"
        HTML &= "<td style='width:280px;' align='left'><font class='White-Style' style='color:#000000'>TOTAL SERVICE CHARGE       (INCLUSIVE OF VAT)</font></td>"
        HTML &= "<td style='width:80px;' valign='bottom' align='right'>&nbsp;</td>"
        HTML &= "<td style='width:80px;' valign='bottom' align='right'>&nbsp;</td>"
        HTML &= "<td style='width:70px;' valign='bottom' align='center'>&nbsp;</td>"
        HTML &= "<td align='right'  valign='bottom'><font class='White-Style' style='color:#000000'>" & FormatNumber((Value + ValueVat).ToString("###############.00"), 2) & "</font></td>"
        HTML &= "</tr>"

        HTML &= "</table>"

        Return HTML
    End Function

    Public Sub HeadReport(ByVal Booking_no As String)
        Dim dt As DataTable = clsGet.Get_BookingOneRecord(Booking_no).Tables(0)
        If (dt.Rows.Count > 0) Then
            lbShip.Text = "SHIP NAME : " & dt.Rows(0).Item("Vehicle_Text")
            lbAgent.Text = "SHIP AGENT : " & dt.Rows(0).Item("MAgent_name")
            lbLoA.Text = "L.O.A.(ft) : " & dt.Rows(0).Item("Vehicle_LOA")
            lbGrt.Text = "GRT : " & dt.Rows(0).Item("Vehicle_GRT")
            lbdWT.Text = "DWT : " & dt.Rows(0).Item("Vehicle_DWT")
            Dim PORT As String = dt.Rows(0).Item("PORT")
            Dim LAYTIME As String = dt.Rows(0).Item("Lay_Time")
            Dim Berthing_Date As String = ""
            Dim Berthing_TimeTo As String = ""
            Dim POB_Date As String = ""
            Dim POB_Time As String = ""
            Dim Date_From As String = dt.Rows(0).Item("Date_From")
            Dim Date_To As String = dt.Rows(0).Item("Date_To")
            Dim Disconnect_Date As String = ""
            Dim Disconnect_to As String = ""

            dt = clsGet.Get_BookingWharf(Booking_no)
            lbWharf.Text = "WHARF No : " & dt.Rows(0).Item("Wharf")
            dt = clsGet.Get_Load_Unload_Product(Booking_no, "")
            Dim PO As String = "0"
            For Each dr As DataRow In dt.Rows
                Try
                    Disconnect_Date = dr.Item("LU_Disconnect_Date")
                    Disconnect_to = dr.Item("LU_Disconnect_To")
                    If PORT = "L" Then
                        PO = CDbl(PO) + CDbl(dr.Item("LU_SLMT"))
                    Else
                        PO = CDbl(PO) + CDbl(dr.Item("LU_SUMT"))
                    End If
                Catch ex As Exception

                End Try
            Next
            lbPo.Text = "PO : " & PO & " MT"
            If (LAYTIME <> "0") Then
                Dim dtLogIn As DataTable = clsGet.Get_Booking_LogSheet_In(Booking_no)
                Dim dtProduct As DataTable = clsGet.Get_Booking_Product(Booking_no)
                Dim dsBooking As DataSet = clsGet.Get_BookingOneRecord(Booking_no)
                If (PORT = "L") Then

                    Dim Product_no As String = ""
                    Try
                        Dim sort As Integer = 0
                        For Each dr As DataRow In dtProduct.Rows
                            If (CInt(dr.Item("Product_Index") & "") > sort) Then
                                sort = CInt(dr.Item("Product_Index"))
                                Product_no = dr.Item("Product_no")
                            End If
                        Next
                    Catch ex As Exception

                    End Try
                    Dim dtLoad As DataTable = clsGet.Get_Load_Unload_Vessel(Booking_no)
                    Dim NOR As String = ""
                    Dim NOR_Time As String = ""
                    Dim POB As String = ""
                    POB_Time = ""
                    If (dtLoad.Rows.Count > 0) Then
                        If (dtLoad.Rows(0).Item("NOR_Date") & "" <> "") Then
                            NOR = dtLoad.Rows(0).Item("NOR_Date").ToString.Substring(0, 2) & " " & GetNameMonth(dtLoad.Rows(0).Item("NOR_Date").ToString.Substring(3, 2)) & " 20" & dtLoad.Rows(0).Item("NOR_Date").ToString.Substring(6, 2)
                            NOR_Time = dtLoad.Rows(0).Item("NOR_Time") & ""
                        Else
                            NOR = dtLogIn.Rows(0).Item("NOR_Date") & ""
                            NOR_Time = dtLogIn.Rows(0).Item("NOR_Time") & ""
                        End If
                    Else
                        NOR = ""
                        NOR_Time = ""
                    End If
                    If (dtLogIn.Rows.Count > 0) Then
                        If (dtLogIn.Rows(0).Item("POB_Date") & "" <> "") Then
                            If (dtLogIn.Rows(0).Item("Up_Date") & "" <> "") Then
                                If (CDate(dtLogIn.Rows(0).Item("POB_Date")) < CDate(dtLogIn.Rows(0).Item("Up_Date"))) Then
                                    POB = dtLogIn.Rows(0).Item("Up_Date") & ""
                                    POB_Time = dtLogIn.Rows(0).Item("Up_Time") & ""
                                ElseIf (CDate(dtLogIn.Rows(0).Item("POB_Date")) = CDate(dtLogIn.Rows(0).Item("Up_Date"))) Then
                                    If (CInt(dtLogIn.Rows(0).Item("POB_Time").ToString().Split(":")(0)) < CInt(dtLogIn.Rows(0).Item("Up_Time").ToString().Split(":")(0))) Then
                                        POB = dtLogIn.Rows(0).Item("Up_Date") & ""
                                        POB_Time = dtLogIn.Rows(0).Item("Up_Time") & ""
                                    ElseIf (CInt(dtLogIn.Rows(0).Item("POB_Time").ToString().Split(":")(0)) = CInt(dtLogIn.Rows(0).Item("Up_Time").ToString().Split(":")(0))) Then
                                        If (CInt(dtLogIn.Rows(0).Item("POB_Time").ToString().Split(":")(1)) < CInt(dtLogIn.Rows(0).Item("Up_Time").ToString().Split(":")(1))) Then
                                            POB = dtLogIn.Rows(0).Item("Up_Date") & ""
                                            POB_Time = dtLogIn.Rows(0).Item("Up_Time") & ""
                                        Else
                                            POB = dtLogIn.Rows(0).Item("POB_Date") & ""
                                            POB_Time = dtLogIn.Rows(0).Item("POB_Time") & ""
                                        End If
                                    Else
                                        POB = dtLogIn.Rows(0).Item("POB_Date") & ""
                                        POB_Time = dtLogIn.Rows(0).Item("POB_Time") & ""
                                    End If
                                Else
                                    POB = dtLogIn.Rows(0).Item("POB_Date") & ""
                                    POB_Time = dtLogIn.Rows(0).Item("POB_Time") & ""
                                End If
                            Else
                                POB = dtLogIn.Rows(0).Item("POB_Date") & ""
                                POB_Time = dtLogIn.Rows(0).Item("POB_Time") & ""
                            End If
                        Else
                            POB = dtLogIn.Rows(0).Item("Up_Date") & ""
                            POB_Time = dtLogIn.Rows(0).Item("Up_Time") & ""
                        End If
                    Else
                        POB = ""
                        POB_Time = ""
                    End If


                    dtLoad = clsGet.Get_Load_Unload_Product(Booking_no, Product_no)

                    Dim Berthing_DateL As String = dtLogIn.Rows(0).Item("Berthing_Date")
                    Try
                        Try
                            If (CInt(dtLogIn.Rows(0).Item("Berthing_TimeTo").ToString().Split(":")(0)) < CInt(dtLogIn.Rows(0).Item("Berthing_TimeFrom").ToString.Split(":")(0))) Then
                                Berthing_Date = CDate(Berthing_Date).AddDays(1).ToString("dd MMM", New CultureInfo("en-US")) & " " & Berthing_Date.Split(" ")(2)
                            End If
                        Catch ex As Exception

                        End Try
                    Catch ex As Exception

                    End Try
                    If (dtLoad.Rows.Count > 0) Then
                        LAYTIME = clsGet.ComputeRemainLaytime(Berthing_DateL, dtLogIn.Rows(0).Item("Berthing_TimeTo"), dsBooking.Tables(0).Rows(0).Item("Lay_Time"), dsBooking.Tables(0).Rows(0).Item("Date_From"), dsBooking.Tables(0).Rows(0).Item("Date_To"), POB, POB_Time, dtLoad.Rows(0).Item("LU_Disconnect_Date") & "", dtLoad.Rows(0).Item("LU_Disconnect_To") & "", NOR, NOR_Time)
                    Else
                        LAYTIME = clsGet.ComputeRemainLaytime(Berthing_DateL, dtLogIn.Rows(0).Item("Berthing_TimeTo"), dsBooking.Tables(0).Rows(0).Item("Lay_Time"), dsBooking.Tables(0).Rows(0).Item("Date_From"), dsBooking.Tables(0).Rows(0).Item("Date_To"), POB, POB_Time, "", "", NOR, NOR_Time)
                    End If

                Else
                    'Bulk
                    Dim dtProductBulk As DataTable = clsGet.Get_Booking_Product(Booking_no)
                    If (dtProductBulk.Rows.Count > 0) Then
                        Dim chkCoil As Boolean = False
                        For Each dx As DataRow In dtProductBulk.Rows
                            If (dx.Item("Group_no") & "" = "00007") Then
                                chkCoil = True
                                Exit For
                            End If
                        Next
                        If (chkCoil) Then
                            Dim LY As Integer = CInt(dsBooking.Tables(0).Rows(0).Item("Lay_Time"))
                            dtProductBulk = clsGet.Get_Load_Unload_Vessel_Product_Detail(Booking_no)
                            If (dtProductBulk.Rows.Count > 0) Then
                                For Each dr As DataRow In dtProductBulk.Rows
                                    Dim ts = DateDiff("n", dr.Item("LU_StartActual_Date").ToString.Substring(0, 2) & " " & GetNameMonth(dr.Item("LU_StartActual_Date").ToString.Substring(3, 2)) & " 20" & dr.Item("LU_StartActual_Date").ToString.Substring(6, 2) & " " & dr.Item("LU_StartActual_Time"), dr.Item("LU_StopActual_Date").ToString.Substring(0, 2) & " " & GetNameMonth(dr.Item("LU_StopActual_Date").ToString.Substring(3, 2)) & " 20" & dr.Item("LU_StopActual_Date").ToString.Substring(6, 2) & " " & dr.Item("LU_StopActual_Time"))
                                    Dim Hour As String = "0"
                                    Dim chksub As String = ""
                                    Dim Minute As String = "0"
                                    If (LY >= (ts \ 60)) Then
                                        If (LY = (ts \ 60)) Then
                                            If (ts Mod 60) > 0 Then
                                                chksub = "-"
                                                Hour = ((ts \ 60) - LY)
                                                Minute = (ts Mod 60)
                                            Else
                                                If (ts Mod 60) > 0 Then
                                                    Minute = 60 - (ts Mod 60)
                                                    LY -= 1
                                                End If
                                                Hour = LY - (ts \ 60)
                                            End If
                                        Else
                                            If (ts Mod 60) > 0 Then
                                                Minute = 60 - (ts Mod 60)
                                                LY -= 1
                                            End If
                                            Hour = LY - (ts \ 60)
                                        End If
                                    Else
                                        chksub = "-"
                                        Hour = ((ts \ 60) - LY)
                                        Minute = (ts Mod 60)
                                    End If
                                    LAYTIME = chksub & Hour & ":" & CInt(Minute).ToString("00")

                                Next
                            End If
                        End If
                    End If
                End If
            End If
            dt = clsGet.Get_Booking_LogSheet_In(Booking_no)
            If (dt.Rows.Count > 0) Then
                Try
                    Dim PERIOD As String = dt.Rows(0).Item("Berthing_Date") & " " & dt.Rows(0).Item("Berthing_TimeFrom")
                    Berthing_Date = dt.Rows(0).Item("Berthing_Date")
                    Berthing_TimeTo = dt.Rows(0).Item("Berthing_TimeTo")
                    POB_Date = dt.Rows(0).Item("POB_Date")
                    POB_Time = dt.Rows(0).Item("POB_Time")
                    dt = clsGet.Get_Booking_LogSheet_Out(Booking_no)
                    PERIOD &= " - " & dt.Rows(0).Item("Last_Date") & " " & dt.Rows(0).Item("Last_Time")
                    lbPilottime.Text = "PERIOD TIME : " & PERIOD
                Catch ex As Exception
                    lbPilottime.Text = ""
                End Try

            Else
                lbPilottime.Text = ""
            End If

            lbLayTime.Text = "LAYTIME : " & LAYTIME
        Else
            lbShip.Text = "SHIP NAME : "
            lbAgent.Text = "SHIP AGENT : "
            lbLoA.Text = "L.O.A.(ft) : "
            lbGrt.Text = "GRT : "
            lbdWT.Text = "DWT : "
            lbPilottime.Text = "PERIOD TIME : "
            lbLayTime.Text = "LAYTIME : 0"
            lbPo.Text = "PO : 0 MT"
            lbWharf.Text = "WHARF No : "
        End If
    End Sub

    Protected Sub GetOperation(ByVal Booking_no As String)
        Dim dt_Log As DataTable = clsGet.Get_Booking_LogSheet_In(Booking_no)
        If (dt_Log.Rows.Count > 0) Then
            If (dt_Log.Rows(0).Item("NOR_Date") & "" <> "") Then
                lbNORSHOW.Text = "NOR Tender :"
                imgNOR.ImageUrl = "../Image/Correct.png"
            ElseIf (dt_Log.Rows(0).Item("ETA_Date") & "" <> "") Then
                lbNORSHOW.Text = "ETA Tender :"
                imgNOR.ImageUrl = "../Image/Correct.png"
            Else
                lbNORSHOW.Text = "NOR Tender :"
                imgNOR.ImageUrl = "../Image/NotCorrect.png"
            End If
            Dim dsBooking As DataTable = clsGet.Get_BookingOneRecord(Booking_no).Tables(0)
            If (dsBooking.Rows.Count > 0) Then
                If (dsBooking.Rows(0).Item("Lay_Time") & "" = "0") Then
                    rowPiliot.Visible = False
                Else
                    If (dt_Log.Rows(0).Item("POB_Date") & "" <> "") Then
                        lbPilot.Text = "Pliot on board"
                        imgPilot.ImageUrl = "../Image/Correct.png"
                    ElseIf (dt_Log.Rows(0).Item("Up_Date") & "" <> "") Then
                        lbPilot.Text = "Anchor Up :"
                        imgPilot.ImageUrl = "../Image/Correct.png"
                    Else
                        lbPilot.Text = "Pliot on board"
                        imgPilot.ImageUrl = "../Image/NotCorrect.png"
                    End If
                End If
            Else
                rowPiliot.Visible = False
            End If

            If (dt_Log.Rows(0).Item("Berthing_Date") & "" <> "") Then
                imgBerth.ImageUrl = "../Image/Correct.png"
            Else
                imgBerth.ImageUrl = "../Image/NotCorrect.png"
            End If
            dt_Log = clsGet.Get_Load_Unload_Product(Booking_no, "")
            Dim p_start As Boolean = False
            Dim p_stop As Boolean = True
            If (dt_Log.Rows.Count > 0) Then
                For Each dr As DataRow In dt_Log.Rows
                    If (dr.Item("LU_StartActual_Date") & "" <> "") Then
                        p_start = True
                    End If
                    If (dr.Item("LU_StopActual_Date") & "" = "") Then
                        p_stop = False
                    End If
                Next
                If (p_start) And (p_stop) Then
                    imgStart.ImageUrl = "../Image/Correct.png"
                    imgFinish.ImageUrl = "../Image/Correct.png"

                    dt_Log = clsGet.Get_Booking_LogSheet_Out(Booking_no)
                    If (dt_Log.Rows.Count > 0) Then
                        If (dt_Log.Rows(0).Item("Last_Date") & "" <> "") Then
                            imgDepart.ImageUrl = "../Image/Correct.png"
                        Else
                            imgDepart.ImageUrl = "../Image/NotCorrect.png"
                        End If
                    Else
                        imgDepart.ImageUrl = "../Image/NotCorrect.png"
                    End If
                ElseIf (p_start) Then
                    imgStart.ImageUrl = "../Image/Correct.png"
                    imgFinish.ImageUrl = "../Image/NotCorrect.png"
                Else
                    imgStart.ImageUrl = "../Image/NotCorrect.png"
                    imgFinish.ImageUrl = "../Image/NotCorrect.png"
                    imgDepart.ImageUrl = "../Image/NotCorrect.png"
                End If
            Else
                imgStart.ImageUrl = "../Image/NotCorrect.png"
                imgFinish.ImageUrl = "../Image/NotCorrect.png"
                imgDepart.ImageUrl = "../Image/NotCorrect.png"
            End If
        Else
            imgNOR.ImageUrl = "../Image/NotCorrect.png"
            imgPilot.ImageUrl = "../Image/NotCorrect.png"
            imgBerth.ImageUrl = "../Image/NotCorrect.png"
            imgStart.ImageUrl = "../Image/NotCorrect.png"
            imgFinish.ImageUrl = "../Image/NotCorrect.png"
            imgDepart.ImageUrl = "../Image/NotCorrect.png"
            lbPilot.Text = "Pilot on board :"
        End If
    End Sub

    Private Function GetNameMonth(ByVal Month As String) As String
        Try
            Select Case (CInt(Month).ToString("00"))
                Case "01"
                    Return "Jan"
                Case "02"
                    Return "Feb"
                Case "03"
                    Return "Mar"
                Case "04"
                    Return "Apr"
                Case "05"
                    Return "May"
                Case "06"
                    Return "Jun"
                Case "07"
                    Return "Jul"
                Case "08"
                    Return "Aug"
                Case "09"
                    Return "Sep"
                Case "10"
                    Return "Oct"
                Case "11"
                    Return "Nov"
                Case "12"
                    Return "Dec"
                Case Else
                    Return ""
            End Select
        Catch ex As Exception
            Return Month
        End Try

    End Function
    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub

    Private Sub GridSub_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridSub.RowCommand
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        If (gridTracking.PageIndex <> 0) Then
            Index = Index Mod (gridTracking.PageIndex * 10)
        End If
        If e.CommandName = "Operation" Then
            panelMain.Visible = False
            PanelSub.Visible = True
            PanelOperation.Visible = True
            panelActual.Visible = False
            GetOperation(GridSub.DataKeys(Index).Values("Booking_no") & "")
        ElseIf (e.CommandName = "Actual") Then
            panelMain.Visible = False
            PanelSub.Visible = True
            panelActual.Visible = True
            PanelOperation.Visible = False
            LiActual.Text = GetActual(GridSub.DataKeys(Index).Values("Booking_no") & "", GridSub.DataKeys(Index).Values("PORT") & "", GridSub.DataKeys(Index).Values("MAgent_no") & "")
        ElseIf (e.CommandName = "Finish") Then

        End If
    End Sub

    Private Sub GridSub_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridSub.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            Dim imgOperation As ImageButton = CType(e.Row.FindControl("imgOperation"), ImageButton)
            Dim imgActual As ImageButton = CType(e.Row.FindControl("imgActual"), ImageButton)
            Dim imgFinish As ImageButton = CType(e.Row.FindControl("imgFinish"), ImageButton)

            'Operation
            Dim chkYellow As Boolean = False
            Dim chkCorrect As Boolean = True
            Dim dt_Log As DataTable = clsGet.Get_Booking_LogSheet_In(GridSub.DataKeys(e.Row.RowIndex).Values("Booking_no") & "")
            If (dt_Log.Rows.Count > 0) Then
                If (dt_Log.Rows(0).Item("NOR_Date") & "" <> "") Then
                    chkYellow = True
                ElseIf (dt_Log.Rows(0).Item("NOR_Date") & "" <> "") Then
                    chkYellow = True
                Else
                    chkCorrect = False
                End If
                Dim dsBooking As DataTable = clsGet.Get_BookingOneRecord(GridSub.DataKeys(e.Row.RowIndex).Values("Booking_no") & "").Tables(0)
                If (dsBooking.Rows.Count > 0) Then
                    If (dsBooking.Rows(0).Item("Lay_Time") & "" = "0") Then
                        rowPiliot.Visible = False
                    Else
                        If (dt_Log.Rows(0).Item("POB_Date") & "" <> "") Then
                            chkYellow = True
                        ElseIf (dt_Log.Rows(0).Item("Up_Date") & "" <> "") Then
                            chkYellow = True
                        Else
                            chkCorrect = False
                        End If
                    End If
                End If
                If (dt_Log.Rows(0).Item("Berthing_Date") & "" <> "") Then
                    chkYellow = True
                Else
                    chkCorrect = False
                End If
                dt_Log = clsGet.Get_Load_Unload_Product(GridSub.DataKeys(e.Row.RowIndex).Values("Booking_no") & "", "")
                Dim p_start As Boolean = False
                Dim p_stop As Boolean = True
                If (dt_Log.Rows.Count > 0) Then
                    For Each dr As DataRow In dt_Log.Rows
                        If (dr.Item("LU_StartActual_Date") & "" <> "") Then
                            p_start = True
                        End If
                        If (dr.Item("LU_StopActual_Date") & "" = "") Then
                            p_stop = False
                        End If
                    Next
                    If (p_start) And (p_stop) Then

                        dt_Log = clsGet.Get_Booking_LogSheet_Out(GridSub.DataKeys(e.Row.RowIndex).Values("Booking_no") & "")
                        If (dt_Log.Rows.Count > 0) Then
                            If (dt_Log.Rows(0).Item("Last_Date") & "" <> "") Then
                                chkYellow = True
                            Else
                                chkCorrect = False
                            End If
                        Else
                            chkCorrect = False
                        End If
                    ElseIf (p_start) Then
                        chkYellow = True
                        chkCorrect = False
                    Else
                        chkCorrect = False
                    End If
                Else
                    chkCorrect = False
                End If
            Else
                chkCorrect = False
            End If

            If (chkCorrect) Then
                imgOperation.ImageUrl = "../Image/Correct.png"
            ElseIf (chkYellow) Then
                imgOperation.ImageUrl = "../Image/Yellow.png"
            Else
                imgOperation.ImageUrl = "../Image/NotCorrect.png"
            End If
            If (GridSub.DataKeys(e.Row.RowIndex).Values("Status") & "" = "FN") Then
                imgActual.ImageUrl = "../Image/Correct.png"
                imgFinish.ImageUrl = "../Image/Correct.png"
            Else
                imgActual.ImageUrl = "../Image/NotCorrect.png"
                imgFinish.ImageUrl = "../Image/NotCorrect.png"
            End If

            If (GridSub.DataKeys(e.Row.RowIndex).Values("Date_From") & "" <> "") Then
                If (GridSub.DataKeys(e.Row.RowIndex).Values("Date_From") & "" = GridSub.DataKeys(e.Row.RowIndex).Values("Date_To") & "") Then
                    e.Row.Cells(2).Text = GridSub.DataKeys(e.Row.RowIndex).Values("Date_To")
                Else
                    e.Row.Cells(2).Text = GridSub.DataKeys(e.Row.RowIndex).Values("Date_From").ToString.Substring(0, 2) & "-" & GridSub.DataKeys(e.Row.RowIndex).Values("Date_To")
                End If
            End If
        End If
    End Sub

    Public Sub imgClose_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgClose.Click
        PanelOperation.Visible = False
        panelActual.Visible = False
        PanelSub.Visible = False
        panelMain.Visible = True
        LoadDataTracking("", "", "", "")
    End Sub

    Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Dim dateFrom As String = ""
        Dim dateTo As String = ""
        If (txtDateSearch.Text <> "") Then
            dateFrom = txtDateSearch.Text.Split(" ")(2) & CDate(txtDateSearch.Text).ToString("MMdd")
        End If
        Dim date_to As String = ""
        If (txtDateToSearch.Text <> "") Then
            dateTo = txtDateToSearch.Text.Split(" ")(2) & CDate(txtDateToSearch.Text).ToString("MMdd")
        End If
        LoadDataTracking(txtBooking_no.Text, txtVessel.Text, dateFrom, dateTo)
    End Sub

    Private Function CreateReport() As String
        Dim service As New _ReportingService.Report
        Dim Orientation As Boolean = True
        Dim ReportName As String = String.Empty
        Dim reportDataSource As String = String.Empty
        Dim paraName As String = String.Empty
        Dim paraValue As String = String.Empty
        Dim reportType As String = String.Empty
        Dim urlDisplay As String = String.Empty
        Dim dsReport As New DataSet

        Dim Vat As String = ""
        Dim dsConfig As DataSet = Session("Config")
        If (dsConfig.Tables(0).Rows(0).Item("Vat").ToString().Split(".")(1) = "00") Then
            Vat = CInt(dsConfig.Tables(0).Rows(0).Item("Vat").ToString().Split(".")(0)).ToString()
        Else
            Vat = dsConfig.Tables(0).Rows(0).Item("Vat").ToString()
        End If


        Dim dt As DataTable
        If (ViewState("PORT") & "" = "B") Then
            dt = clsGet.GetActual_Data_Backup_For_Billing(ViewState("EditBooking"), ViewState("SoldTo"), False)
        Else
            dt = clsGet.GetActual_Data_Backup_For_Billing(ViewState("EditBooking"), "", False)
        End If
        If dt.Rows.Count > 0 Then

            dt.Columns.Add("VAT", GetType(String))
            dt.Columns.Add("VAT_AMOUNT", GetType(String))
            dt.Columns.Add("NET", GetType(String))
            dt.Columns.Add("AMOUNT", GetType(String))
            dt.Columns.Add("Text_AMOUNT", GetType(String))
            dt.AcceptChanges()
            Dim NET As Double = 0.0
            For i As Integer = 0 To dt.Rows.Count - 1
                NET = NET + CDbl(CDbl(dt.Rows(i).Item("data4")).ToString("##############0.00"))
            Next
            Dim VAT_AMOUNT As String = ((NET * CDbl(Vat)) / 100).ToString("############.00")
            Dim AMOUNT As String = NET + CDbl(((NET * CDbl(Vat)) / 100).ToString("############.00"))
            Dim Text_AMOUNT As String = ConvertMoneyToText((NET + CDbl(((NET * CDbl(Vat)) / 100).ToString("############.00"))).ToString())
            For i As Integer = 0 To dt.Rows.Count - 1
                Dim MAT As String = ""
                Try
                    MAT = CInt(dt.Rows(i).Item("MATERIAL"))
                Catch ex As Exception
                    MAT = dt.Rows(i).Item("MATERIAL")
                End Try
                dt.Rows(i).Item("MATERIAL") = MAT
                dt.Rows(i).Item("VAT") = Vat
                dt.Rows(i).Item("VAT_AMOUNT") = FormatNumber(VAT_AMOUNT, 2)
                dt.Rows(i).Item("NET") = FormatNumber(NET, 2)
                dt.Rows(i).Item("AMOUNT") = FormatNumber(AMOUNT, 2)
                dt.Rows(i).Item("Text_AMOUNT") = Text_AMOUNT
                Try
                    dt.Rows(i).Item("data1") = FormatNumber(dt.Rows(i).Item("data1"), 2)
                Catch ex As Exception

                End Try
                Try
                    dt.Rows(i).Item("data2") = FormatNumber(dt.Rows(i).Item("data2"), 2)
                Catch ex As Exception

                End Try
                Try
                    dt.Rows(i).Item("data3") = FormatNumber(dt.Rows(i).Item("data3"), 2)
                Catch ex As Exception

                End Try
                Try
                    dt.Rows(i).Item("data4") = FormatNumber(dt.Rows(i).Item("data4"), 2)
                Catch ex As Exception

                End Try
                dt.AcceptChanges()
            Next
            dsReport.Tables.Add(dt.Copy)
            dsReport.AcceptChanges()
            ReportName = "Actual_Report.rdlc"
            ' dsReport = New DataSet
            reportDataSource = "Actual_Report_Actual"

            dt = clsGet.Get_BookingOneRecord(ViewState("EditBooking")).Tables(0)


            paraName = "SHIP;PERIOD;LAYTIME;AGENT;LOA;GRT;DWT;PO;WHARF;Booking_no;Estimate"
            If (dt.Rows.Count > 0) Then
                Dim SHip As String = dt.Rows(0).Item("Vehicle_Text")
                Dim AGENT As String = dt.Rows(0).Item("MAgent_name")
                Dim LOA As String = dt.Rows(0).Item("Vehicle_LOA")
                Dim GRT As String = dt.Rows(0).Item("Vehicle_GRT")
                Dim DWT As String = dt.Rows(0).Item("Vehicle_DWT")
                Dim PORT As String = dt.Rows(0).Item("PORT")
                Dim LAYTIME As String = dt.Rows(0).Item("Lay_Time")
                Dim Berthing_Date As String = ""
                Dim Berthing_TimeTo As String = ""
                Dim POB_Date As String = ""
                Dim POB_Time As String = ""
                Dim Date_From As String = dt.Rows(0).Item("Date_From")
                Dim Date_To As String = dt.Rows(0).Item("Date_To")
                Dim Disconnect_Date As String = ""
                Dim Disconnect_to As String = ""

                dt = clsGet.Get_BookingWharf(ViewState("EditBooking"))
                Dim WHARF As String = dt.Rows(0).Item("Wharf")
                dt = clsGet.Get_Load_Unload_Product(ViewState("EditBooking"), "")
                Dim PO As String = "0"
                For Each dr As DataRow In dt.Rows
                    Try
                        Disconnect_Date = dr.Item("LU_Disconnect_Date")
                        Disconnect_to = dr.Item("LU_Disconnect_To")
                        If PORT = "L" Then
                            PO = CDbl(PO) + CDbl(dr.Item("LU_SLMT"))
                        Else
                            PO = CDbl(PO) + CDbl(dr.Item("LU_SUMT"))
                        End If
                    Catch ex As Exception

                    End Try
                Next
                If (LAYTIME <> "0") Then
                    Dim dtLogIn As DataTable = clsGet.Get_Booking_LogSheet_In(ViewState("EditBooking"))
                    Dim dtProduct As DataTable = clsGet.Get_Booking_Product(ViewState("EditBooking"))
                    Dim dsBooking As DataSet = clsGet.Get_BookingOneRecord(ViewState("EditBooking"))
                    If (PORT = "L") Then

                        Dim Product_no As String = ""
                        Try
                            Dim sort As Integer = 0
                            For Each dr As DataRow In dtProduct.Rows
                                If (CInt(dr.Item("Product_Index") & "") > sort) Then
                                    sort = CInt(dr.Item("Product_Index"))
                                    Product_no = dr.Item("Product_no")
                                End If
                            Next
                        Catch ex As Exception

                        End Try
                        Dim dtLoad As DataTable = clsGet.Get_Load_Unload_Vessel(ViewState("EditBooking"))
                        Dim NOR As String = ""
                        Dim NOR_Time As String = ""
                        Dim POB As String = ""
                        POB_Time = ""
                        If (dtLoad.Rows.Count > 0) Then
                            If (dtLoad.Rows(0).Item("NOR_Date") & "" <> "") Then
                                NOR = dtLoad.Rows(0).Item("NOR_Date").ToString.Substring(0, 2) & " " & GetNameMonth(dtLoad.Rows(0).Item("NOR_Date").ToString.Substring(3, 2)) & " 20" & dtLoad.Rows(0).Item("NOR_Date").ToString.Substring(6, 2)
                                NOR_Time = dtLoad.Rows(0).Item("NOR_Time") & ""
                            Else
                                NOR = dtLogIn.Rows(0).Item("NOR_Date") & ""
                                NOR_Time = dtLogIn.Rows(0).Item("NOR_Time") & ""
                            End If
                        Else
                            NOR = dtLogIn.Rows(0).Item("NOR_Date") & ""
                            NOR_Time = dtLogIn.Rows(0).Item("NOR_Time") & ""
                        End If
                        If (dtLogIn.Rows(0).Item("POB_Date") & "" <> "") Then
                            If (dtLogIn.Rows(0).Item("Up_Date") & "" <> "") Then
                                If (CDate(dtLogIn.Rows(0).Item("POB_Date")) < CDate(dtLogIn.Rows(0).Item("Up_Date"))) Then
                                    POB = dtLogIn.Rows(0).Item("Up_Date") & ""
                                    POB_Time = dtLogIn.Rows(0).Item("Up_Time") & ""
                                ElseIf (CDate(dtLogIn.Rows(0).Item("POB_Date")) = CDate(dtLogIn.Rows(0).Item("Up_Date"))) Then
                                    If (CInt(dtLogIn.Rows(0).Item("POB_Time").ToString().Split(":")(0)) < CInt(dtLogIn.Rows(0).Item("Up_Time").ToString().Split(":")(0))) Then
                                        POB = dtLogIn.Rows(0).Item("Up_Date") & ""
                                        POB_Time = dtLogIn.Rows(0).Item("Up_Time") & ""
                                    ElseIf (CInt(dtLogIn.Rows(0).Item("POB_Time").ToString().Split(":")(0)) = CInt(dtLogIn.Rows(0).Item("Up_Time").ToString().Split(":")(0))) Then
                                        If (CInt(dtLogIn.Rows(0).Item("POB_Time").ToString().Split(":")(1)) < CInt(dtLogIn.Rows(0).Item("Up_Time").ToString().Split(":")(1))) Then
                                            POB = dtLogIn.Rows(0).Item("Up_Date") & ""
                                            POB_Time = dtLogIn.Rows(0).Item("Up_Time") & ""
                                        Else
                                            POB = dtLogIn.Rows(0).Item("POB_Date") & ""
                                            POB_Time = dtLogIn.Rows(0).Item("POB_Time") & ""
                                        End If
                                    Else
                                        POB = dtLogIn.Rows(0).Item("POB_Date") & ""
                                        POB_Time = dtLogIn.Rows(0).Item("POB_Time") & ""
                                    End If
                                Else
                                    POB = dtLogIn.Rows(0).Item("POB_Date") & ""
                                    POB_Time = dtLogIn.Rows(0).Item("POB_Time") & ""
                                End If
                            Else
                                POB = dtLogIn.Rows(0).Item("POB_Date") & ""
                                POB_Time = dtLogIn.Rows(0).Item("POB_Time") & ""
                            End If
                        Else
                            POB = dtLogIn.Rows(0).Item("Up_Date") & ""
                            POB_Time = dtLogIn.Rows(0).Item("Up_Time") & ""
                        End If

                        dtLoad = clsGet.Get_Load_Unload_Product(ViewState("EditBooking"), Product_no)

                        Dim Berthing_DateL As String = dtLogIn.Rows(0).Item("Berthing_Date")
                        Try
                            Try
                                If (CInt(dtLogIn.Rows(0).Item("Berthing_TimeTo").ToString().Split(":")(0)) < CInt(dtLogIn.Rows(0).Item("Berthing_TimeFrom").ToString.Split(":")(0))) Then
                                    Berthing_Date = CDate(Berthing_Date).AddDays(1).ToString("dd MMM", New CultureInfo("en-US")) & " " & Berthing_Date.Split(" ")(2)
                                End If
                            Catch ex As Exception

                            End Try
                        Catch ex As Exception

                        End Try
                        If (dtLoad.Rows.Count > 0) Then
                            LAYTIME = clsGet.ComputeRemainLaytime(Berthing_DateL, dtLogIn.Rows(0).Item("Berthing_TimeTo"), dsBooking.Tables(0).Rows(0).Item("Lay_Time"), dsBooking.Tables(0).Rows(0).Item("Date_From"), dsBooking.Tables(0).Rows(0).Item("Date_To"), POB, POB_Time, dtLoad.Rows(0).Item("LU_Disconnect_Date") & "", dtLoad.Rows(0).Item("LU_Disconnect_To") & "", NOR, NOR_Time)
                        Else
                            LAYTIME = clsGet.ComputeRemainLaytime(Berthing_DateL, dtLogIn.Rows(0).Item("Berthing_TimeTo"), dsBooking.Tables(0).Rows(0).Item("Lay_Time"), dsBooking.Tables(0).Rows(0).Item("Date_From"), dsBooking.Tables(0).Rows(0).Item("Date_To"), POB, POB_Time, "", "", NOR, NOR_Time)
                        End If

                    Else
                        'Bulk
                        Dim dtProductBulk As DataTable = clsGet.Get_Booking_Product(ViewState("EditBooking"))
                        If (dtProductBulk.Rows.Count > 0) Then
                            Dim chkCoil As Boolean = False
                            For Each dx As DataRow In dtProductBulk.Rows
                                If (dx.Item("Group_no") & "" = "00007") Then
                                    chkCoil = True
                                    Exit For
                                End If
                            Next
                            If (chkCoil) Then
                                Dim LY As Integer = CInt(dsBooking.Tables(0).Rows(0).Item("Lay_Time"))
                                dtProductBulk = clsGet.Get_Load_Unload_Vessel_Product_Detail(ViewState("EditBooking"))
                                If (dtProductBulk.Rows.Count > 0) Then
                                    For Each dr As DataRow In dtProductBulk.Rows
                                        Dim ts = DateDiff("n", dr.Item("LU_StartActual_Date").ToString.Substring(0, 2) & " " & GetNameMonth(dr.Item("LU_StartActual_Date").ToString.Substring(3, 2)) & " 20" & dr.Item("LU_StartActual_Date").ToString.Substring(6, 2) & " " & dr.Item("LU_StartActual_Time"), dr.Item("LU_StopActual_Date").ToString.Substring(0, 2) & " " & GetNameMonth(dr.Item("LU_StopActual_Date").ToString.Substring(3, 2)) & " 20" & dr.Item("LU_StopActual_Date").ToString.Substring(6, 2) & " " & dr.Item("LU_StopActual_Time"))
                                        Dim Hour As String = "0"
                                        Dim chksub As String = ""
                                        Dim Minute As String = "0"
                                        If (LY >= (ts \ 60)) Then
                                            If (LY = (ts \ 60)) Then
                                                If (ts Mod 60) > 0 Then
                                                    chksub = "-"
                                                    Hour = ((ts \ 60) - LY)
                                                    Minute = (ts Mod 60)
                                                Else
                                                    If (ts Mod 60) > 0 Then
                                                        Minute = 60 - (ts Mod 60)
                                                        LY -= 1
                                                    End If
                                                    Hour = LY - (ts \ 60)
                                                End If
                                            Else
                                                If (ts Mod 60) > 0 Then
                                                    Minute = 60 - (ts Mod 60)
                                                    LY -= 1
                                                End If
                                                Hour = LY - (ts \ 60)
                                            End If
                                        Else
                                            chksub = "-"
                                            Hour = ((ts \ 60) - LY)
                                            Minute = (ts Mod 60)
                                        End If
                                        LAYTIME = chksub & Hour & ":" & CInt(Minute).ToString("00")

                                    Next
                                End If
                            End If
                        End If
                    End If
                End If
                dt = clsGet.Get_Booking_LogSheet_In(ViewState("EditBooking"))
                Dim PERIOD As String = dt.Rows(0).Item("Berthing_Date") & " " & dt.Rows(0).Item("Berthing_TimeFrom")
                Berthing_Date = dt.Rows(0).Item("Berthing_Date")
                Berthing_TimeTo = dt.Rows(0).Item("Berthing_TimeTo")
                POB_Date = dt.Rows(0).Item("POB_Date")
                POB_Time = dt.Rows(0).Item("POB_Time")
                dt = clsGet.Get_Booking_LogSheet_Out(ViewState("EditBooking"))
                PERIOD &= " - " & dt.Rows(0).Item("Last_Date") & " " & dt.Rows(0).Item("Last_Time")
                paraValue = SHip & ";" & PERIOD & ";" & LAYTIME & ";" & AGENT & ";" & FormatNumber(LOA, 2) & ";" & FormatNumber(GRT, 2) & ";" & FormatNumber(DWT, 2) & ";" & FormatNumber(PO, 2) & " MT" & ";" & WHARF & ";" & ViewState("EditBooking") & ";"
            Else
                paraValue = ";;;;;;;;;"
            End If

            Orientation = True
            reportType = "pdf"

            Try
                urlDisplay = service.CreateReporting(dsReport, reportDataSource, reportType, ReportName, paraName, paraValue, Orientation, "")

                Return urlDisplay
            Catch ex As Exception
                ' lbNet.Text = ex.Message
            End Try
        Else
            Return ""
        End If
    End Function

    Private Function ConvertMoneyToText(ByVal Money As String) As String
        Dim Digit As String = Money.Split(".")(0)
        Dim SubMoney As String
        Try
            SubMoney = Money.Split(".")(1)
        Catch ex As Exception
            SubMoney = "00"
        End Try
        Dim COuntLength As Int16 = Digit.Length

        Dim TxtMoney As String = ""

        For i As Integer = 0 To COuntLength - 1
            If (COuntLength > 7) Then
                Select Case (Digit.Substring(0, 1).ToString())
                    Case "9" : TxtMoney &= "เก้า"
                    Case "8" : TxtMoney &= "แปด"
                    Case "7" : TxtMoney &= "เจ็ด"
                    Case "6" : TxtMoney &= "หก"
                    Case "5" : TxtMoney &= "ห้า"
                    Case "4" : TxtMoney &= "สี่"
                    Case "3" : TxtMoney &= "สาม"
                    Case "2" : TxtMoney &= "สอง"
                    Case "1" : TxtMoney &= "หนึ่ง"
                End Select

                Select Case (COuntLength.ToString)
                    Case "13" : TxtMoney &= "ล้านล้าน"
                    Case "12" : TxtMoney &= "แสนล้าน"
                    Case "11" : TxtMoney &= "หมื่นล้าน"
                    Case "10" : TxtMoney &= "พันล้าน"
                    Case "9" : TxtMoney &= "ร้อยล้าน"
                    Case "8" : TxtMoney &= "สิบล้าน"
                End Select
            Else
                Select Case (Digit.Substring(0, 1).ToString())
                    Case "9" : TxtMoney &= "เก้า"
                    Case "8" : TxtMoney &= "แปด"
                    Case "7" : TxtMoney &= "เจ็ด"
                    Case "6" : TxtMoney &= "หก"
                    Case "5" : TxtMoney &= "ห้า"
                    Case "4" : TxtMoney &= "สี่"
                    Case "3" : TxtMoney &= "สาม"
                    Case "2"
                        If (COuntLength.ToString() = "2") Then
                            TxtMoney &= "ยี่"
                        Else
                            TxtMoney &= "สอง"
                        End If

                    Case "1"
                        If (COuntLength.ToString() = "2") Then
                            TxtMoney &= "เอ็ด"
                        Else
                            TxtMoney &= "หนึ่ง"
                        End If
                End Select

                Select Case (COuntLength.ToString)
                    Case "7" : TxtMoney &= "ล้าน"
                    Case "6" : TxtMoney &= "แสน"
                    Case "5" : TxtMoney &= "หมื่น"
                    Case "4" : TxtMoney &= "พัน"
                    Case "3" : TxtMoney &= "ร้อย"
                    Case "2" : TxtMoney &= "สิบ"
                    Case "1" : TxtMoney &= "บาท"
                End Select
            End If
            Select Case (COuntLength.ToString())
                Case "13"
                    Digit = CInt(Digit) Mod 1000000000000
                Case "12"
                    Digit = CInt(Digit) Mod 100000000000
                Case "11"
                    Digit = CInt(Digit) Mod 10000000000
                Case "10"
                    Digit = CInt(Digit) Mod 1000000000
                Case "9"
                    Digit = CInt(Digit) Mod 100000000
                Case "8"
                    Digit = CInt(Digit) Mod 10000000
                Case "7"
                    Digit = CInt(Digit) Mod 1000000
                Case "6"
                    Digit = CInt(Digit) Mod 100000
                Case "5"
                    Digit = CInt(Digit) Mod 10000
                Case "4"
                    Digit = CInt(Digit) Mod 1000
                Case "3"
                    Digit = CInt(Digit) Mod 100
                Case "2"
                    Digit = CInt(Digit) Mod 10
                Case "1"
                    Digit = CInt(Digit) Mod 1
            End Select
            COuntLength -= 1
        Next
        If (SubMoney <> "00") Then
            If (SubMoney.Length = 2) Then
                Select Case (SubMoney.Substring(0, 1))
                    Case "9" : TxtMoney &= "เก้าสิบ"
                    Case "8" : TxtMoney &= "แปดสิบ"
                    Case "7" : TxtMoney &= "เจ็ดสิบ"
                    Case "6" : TxtMoney &= "หกสิบ"
                    Case "5" : TxtMoney &= "ห้าสิบ"
                    Case "4" : TxtMoney &= "สี่สิบ"
                    Case "3" : TxtMoney &= "สามสิบ"
                    Case "2" : TxtMoney &= "ยี่สิบ"
                    Case "1" : TxtMoney &= "สิบ"
                End Select
                Select Case (SubMoney.Substring(1, 1))
                    Case "9" : TxtMoney &= "เก้าสตางค์"
                    Case "8" : TxtMoney &= "แปดสตางค์"
                    Case "7" : TxtMoney &= "เจ็ดสตางค์"
                    Case "6" : TxtMoney &= "หกสตางค์"
                    Case "5" : TxtMoney &= "ห้าสตางค์"
                    Case "4" : TxtMoney &= "สี่สตางค์"
                    Case "3" : TxtMoney &= "สามสตางค์"
                    Case "2" : TxtMoney &= "สองสตางค์"
                    Case "1"
                        If (SubMoney.Length = 2) Then
                            If (SubMoney.Substring(0, 1) = "0") Then
                                TxtMoney &= "หนึ่งสตางค์"
                            Else
                                TxtMoney &= "เอ็ดสตางค์"
                            End If

                        Else
                            TxtMoney &= "หนึ่งสตางค์"
                        End If
                End Select
            Else
                Select Case (SubMoney.Substring(0, 1))
                    Case "9" : TxtMoney &= "เก้าสิบสตางค์"
                    Case "8" : TxtMoney &= "แปดสิบสตางค์"
                    Case "7" : TxtMoney &= "เจ็ดสิบสตางค์"
                    Case "6" : TxtMoney &= "หกสิบสตางค์"
                    Case "5" : TxtMoney &= "ห้าสิบสตางค์"
                    Case "4" : TxtMoney &= "สี่สิบสตางค์"
                    Case "3" : TxtMoney &= "สามสิบสตางค์"
                    Case "2" : TxtMoney &= "ยี่สิบสตางค์"
                    Case "1" : TxtMoney &= "สิบสตางค์"
                End Select
            End If
        End If
        Return TxtMoney
    End Function

    Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnPrint.Click
        Dim optWindow As String = "left=0,top=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no"
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "window.open('" + CreateReport() + "','_blank','" + optWindow + "');", True)
    End Sub

    Private Sub btnSendMailActual_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnSendMailActual.Click
        Dim test As String = clsFc.sendmail(clsGet.Get_EmailForSendAgent(ViewState("EditBooking")), "", "", GenBodyMailEstimate(), GetPath())
        'Dim test As String = clsFc.sendmail("kunsky_065@hotmail.com", "", "", GenBodyMailEstimate(), GetPath())
        If (test = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ส่ง E-Mail สำเร็จ !!');", True)
        Else
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('" & test & " !!');", True)
        End If
    End Sub

    Private Function GetPath() As String
        Dim path As String = CreateReport()
        If (path <> "") Then
            Dim str() As String = Split(path, "WriteReport")
            path = Server.MapPath("..") & "/WriteReport" & str(str.Length - 1)
        End If
        Return path
    End Function

    Private Function GenBodyMailEstimate() As String
        Dim HTML As String = ""
        HTML = "<table width='100%' border='0' cellpadding='2' cellspacing='1'>"
        HTML &= "<tr>"
        HTML &= "<td align='center'><font class='White-Style' font-weight:bold style='color:#000000'>รายละเอียดค่าใช้บริการในท่าเรือ !!</font></td>"
        HTML &= "</tr>"
        HTML &= "</table>"
        Return HTML
    End Function

    Private Function GenBodyMailOperation(ByVal Booking_no) As String
        Dim HTML As String = ""
        Dim dt_Log As DataTable = clsGet.Get_Booking_LogSheet_In(Booking_no)
        Dim NOR As String = ""
        Dim Pilot As String = ""
        Dim Berth As String = ""
        Dim Start As String = ""
        Dim Finish As String = ""
        Dim Depart As String = ""
        If (dt_Log.Rows.Count > 0) Then
            If (dt_Log.Rows(0).Item("NOR_Date") & "" <> "") Then
                NOR = "Finish"
            ElseIf (dt_Log.Rows(0).Item("ETA_Date") & "" <> "") Then
                NOR = "Finish"
            Else
                NOR = "Not Start"
            End If
            If (dt_Log.Rows(0).Item("POB_Date") & "" <> "") Then
                Pilot = "Finish"
            ElseIf (dt_Log.Rows(0).Item("Up_Date") & "" <> "") Then
                Pilot = "Finish"
            Else
                Pilot = "Not Start"
            End If
            If (dt_Log.Rows(0).Item("Berthing_Date") & "" <> "") Then
                Berth = "Finish"
            Else
                Berth = "Not Start"
            End If
            dt_Log = clsGet.Get_Load_Unload_Product(Booking_no, "")
            Dim p_start As Boolean = False
            Dim p_stop As Boolean = True
            If (dt_Log.Rows.Count > 0) Then
                For Each dr As DataRow In dt_Log.Rows
                    If (dr.Item("LU_StartActual_Date") & "" <> "") Then
                        p_start = True
                    End If
                    If (dr.Item("LU_StopActual_Date") & "" = "") Then
                        p_stop = False
                    End If
                Next
                If (p_start) And (p_stop) Then
                    Start = "Finish"
                    Finish = "Finish"

                    dt_Log = clsGet.Get_Booking_LogSheet_Out(Booking_no)
                    If (dt_Log.Rows.Count > 0) Then
                        If (dt_Log.Rows(0).Item("Last_Date") & "" <> "") Then
                            Depart = "Finish"
                        Else
                            Depart = "Not Start"
                        End If
                    Else
                        Depart = "Not Start"
                    End If
                ElseIf (p_start) Then
                    Start = "Finish"
                    Finish = "Not Start"
                Else
                    Start = "Not Start"
                    Finish = "Not Start"
                    Depart = "Not Start"
                End If
            Else
                Start = "Not Start"
                Finish = "Not Start"
                Depart = "Not Start"
            End If
        Else
            NOR = "Not Start"
            Pilot = "Not Start"
            Berth = "Not Start"
            Start = "Not Start"
            Finish = "Not Start"
            Depart = "Not Start"
        End If
        HTML = "<table width='100%' border='0' cellpadding='2' cellspacing='1'>"
        HTML &= "<tr>"
        HTML &= "<td style='width:220px;' align='right'><font class='White-Style' style='color:#000000'>" & lbNORSHOW.Text & "</font>&nbsp;</td>"
        HTML &= "<td align='left'><font class='White-Style' style='color:#000000'>" & NOR & "</font></td>"
        HTML &= "</tr>"
        HTML &= "<tr>"
        HTML &= "<td style='width:220px;' align='right'><font class='White-Style' style='color:#000000'>" & lbPilot.Text & "</font>&nbsp;</td>"
        HTML &= "<td align='left'><font class='White-Style' style='color:#000000'>" & Pilot & "</font></td>"
        HTML &= "</tr>"
        HTML &= "<tr>"
        HTML &= "<td style='width:220px;' align='right'><font class='White-Style' style='color:#000000'>" & Label6.Text & "</font>&nbsp;</td>"
        HTML &= "<td align='left'><font class='White-Style' style='color:#000000'>" & Berth & "</font></td>"
        HTML &= "</tr>"
        HTML &= "<tr>"
        HTML &= "<td style='width:220px;' align='right'><font class='White-Style' style='color:#000000'>" & Label8.Text & "</font>&nbsp;</td>"
        HTML &= "<td align='left'><font class='White-Style' style='color:#000000'>" & Start & "</font></td>"
        HTML &= "</tr>"
        HTML &= "<tr>"
        HTML &= "<td style='width:220px;' align='right'><font class='White-Style' style='color:#000000'>" & Label10.Text & "</font>&nbsp;</td>"
        HTML &= "<td align='left'><font class='White-Style' style='color:#000000'>" & Finish & "</font></td>"
        HTML &= "</tr>"
        HTML &= "<tr>"
        HTML &= "<td style='width:220px;' align='right'><font class='White-Style' style='color:#000000'>" & Label4.Text & "</font>&nbsp;</td>"
        HTML &= "<td align='left'><font class='White-Style' style='color:#000000'>" & Depart & "</font></td>"
        HTML &= "</tr>"
        HTML &= "</table>"
        Return HTML
    End Function

    Private Sub imgSendMail_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgSendMail.Click
        Dim test As String = clsFc.sendmail(clsGet.Get_EmailForSendAgent(ViewState("EditBooking")), "", "iPORT", GenBodyMailOperation(ViewState("EditBooking")), "")
        If (test = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ส่ง E-Mail สำเร็จ !!');", True)
        Else
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('" & test & " !!');", True)
        End If
    End Sub
End Class